package com.kitty.game.equip.facade;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.ServerService;
import com.kitty.game.role.model.Role;
import com.kitty.game.equip.handler.UpgradeHandler;
import com.kitty.game.equip.handler.UpgradeHelper;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.equip.message.RespSplitJewelryComplete;
import com.kitty.game.team.message.RespMsg;
import com.kitty.listener.EventType;
import com.kitty.listener.annotation.EventHandler;
import com.kitty.listener.event.UpgradeEquipEvent;
import com.kitty.mina.message.MessagePusher;
import org.nutz.lang.Strings;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
public class UpgradeController {
    /**
     * 处理装备打造事件
     */
    @EventHandler(EventType.UPGRADE_EQUIP)
    public void handleUpgradeEquipEvent(UpgradeEquipEvent upgradeEquipEvent) {
        Role role = upgradeEquipEvent.getRole();
        int pos = upgradeEquipEvent.getUpgradePos();
        String param = upgradeEquipEvent.getUpgradeParam();
        byte upgradeType = upgradeEquipEvent.getUpgradeType();
        if (SpringUtils.getBean(ServerService.class).getServer().getId() == 10000){
            MessagePusher.pushMessage(role,new RespNotifyMiscEx("跨服专线中，不能强化装备。"));
            return;
        }
        UpgradeHandler handler = UpgradeHelper.INSTANCE.getHandler(upgradeType);
        if (handler != null) {
            String message = handler.acceptUpgrade(role, pos, param);
            /**首饰分解单独一个提示协议*/
            if(handler.getUpdateTypeStr()!=null && handler.getUpdateTypeStr().equals("splite_jewelry")){
                RespSplitJewelryComplete splitJewelryComplete = new RespSplitJewelryComplete();
                splitJewelryComplete.setType((byte) 1);
                splitJewelryComplete.setMsg(message);
                MessagePusher.pushMessage(role, splitJewelryComplete);
            }else {
                pushOperateMessage(role, message);
            }
            handler.pushExtraField(role, pos);
        } else {
            MessagePusher.pushMessage(role, new RespMsg("很抱歉，该功能暂未开放。"));
        }
    }

    /**
     * 发送打造返回的消息
     */
    private void pushOperateMessage(Role role, String message) {
        if(!Strings.isEmpty(message)){
            MessagePusher.pushMessage(role, new RespNotifyMiscEx(message));
        }
    }
    static List<Double> result = new ArrayList<>();
    public static void main(String[] args) {
        double[] d = {11933.34,4873.74,4501.99,3269.29,12423.84,1769.35,2833.79,8698.97,4641.49};
        String a = "3058.35,7813.85,4155.95,2046.72,2237.84,3148.79,4973.1,8281.48,4023.2,4525.36,14464.44,2306.64,5346.53,433.14,7194.75,11625.73,4520.48,4970.22,3092.75,5865.99,1667.63,7395.22,1123.51,1616.3,4500.44,2797.2,24.49,2451.22,19152.14,1616.3,21949.34,45.58,11.32,2287.44,21055.16,1685.57,2917.08,241.9";
        diguiSum(a,102557.6);


//        double[] f = {374.47, 6565.88, 12937.49, 194.03, 9541.58, 2414.25, 28711.46, 4014.93, 1340.9, 44060.73, 12449.69, 63405.71, 275.97};
//        double sum = 0;
//        for (int i = 0; i < f.length; i++) {
//            sum+=f[i];
//        }
//        if(result.size()>0){
//            for (double v : result) {
//                System.out.println(v);
//            }
//        }
        //System.out.println(sum);

    }
//
//    public static double[] diguiSum(double[] array,double sum) {
//        for (int i = 0; i < array.length; i++) {
//            double[] cache = new double[i + 1];
//            int ceng = -1;
//            int cengQuit = i;
//            int startPiont = 0;
//            double[] cir = cir(ceng, cengQuit, startPiont, array, cache, sum);
//            //return cir;
//        }
//        return null;
//    }
//
//    // 递归求结果
//    public static double[] cir(int ceng, int cengQuit, int startPiont, double[] array, double[] cache, double sum) {
//        ceng++;
//        for (int i = startPiont; i < array.length; i++) {
//            cache[ceng] = array[i];
//            if (ceng == cengQuit) {
//                if (getSum(cache) == sum) {
//                    for (double v : cache) {
//                        System.out.println(v);
//                        result.add(v);
//                    }
//                    System.out.println("------------------------------------");
//                    //return cache;
//                }
//                if (getSum(cache) > sum) {
//                    break;
//                }
//            }
//            if (ceng < cengQuit) {
//                startPiont = i + 1;
//                cir(ceng, cengQuit, startPiont, array, cache,sum);
//            }
//        }
//        return null;
//    }
//
//    // 获取组合数字之和
//    public static double getSum(double[] cache) {
//        double sum = 0.0;
//        for (int i = 0; i < cache.length; i++) {
//            sum = sum + cache[i];
//        }
//        return sum;
//    }
//
//
//







    public static void diguiSum(String str, double sum) {
        String[] x = str.split(",");
        double[] array = arrayTransform(x);
        for (int i = 0; i < 1000; i++) {
            double[] cache = new double[i + 1];
            int ceng = -1;
            int cengQuit = i;
            int startPiont = 0;
            cir(ceng, cengQuit, startPiont, array, cache, sum);
        }
    }

    // 递归求结果
    public static void cir(int ceng, int cengQuit, int startPiont,
                           double[] array, double[] cache, double sum) {
        ceng++;
        for (int i = startPiont; i < array.length; i++) {
            cache[ceng] = array[i];
            if (ceng == cengQuit) {
                if (getSum(cache) == sum) {
                    printcache(cache);
                }
                if (getSum(cache) > sum) {
                    break;
                }
            }
            if (ceng < cengQuit) {
                startPiont = i + 1;
                cir(ceng, cengQuit, startPiont, array, cache, sum);
            }
        }
    }

    // 获取组合数字之和
    public static double getSum(double[] cache) {
        double sum = 0;
        for (int i = 0; i < cache.length; i++) {
            sum = sum + cache[i];
        }
        return sum;
    }

    // 打印组合的可能
    public static void printcache(double[] cache) {
        for (int i = 0; i < cache.length; i++) {
            System.out.print(cache[i] + ",");
        }
        System.out.println();
    }

    // 转换数组类型 且为提高效率做准备
    public static double[] arrayTransform(String[] strArray) {
        int length = 0;

        double[] array = new double[strArray.length];
        for (int i = 0; i < strArray.length; i++) {
            array[i] = Double.valueOf(strArray[i]);
        }
        Arrays.sort(array);
        for (int i = 0; i < array.length; i++) {
            if (array[i] > 100) {
                length = i;
                break;
            }
        }
        double[] dest = new double[length];
        if (length == 0) {
            return array;
        }
        System.arraycopy(array, 0, dest, 0, length);
        return dest;
    }

}
